import 'package:flutter/material.dart';
import 'package:provider_demo/page/cart_page.dart';
import 'package:provider_demo/page/home_page.dart';

class IndexPageWidget extends StatefulWidget {
  const IndexPageWidget({Key? key}) : super(key: key);

  @override
  State<IndexPageWidget> createState() => _IndexPageWidgetState();
}

class _IndexPageWidgetState extends State<IndexPageWidget> {

  int currentIndex = 0 ;
  late Widget currentWidget;

  List<Widget> tabList = [
    HomePageWidget(),
    CartPageWidget(),
  ];

  List<BottomNavigationBarItem> itemList = [
    const BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: "首页"
    ),
    const BottomNavigationBarItem(
        icon: Icon(Icons.shopping_cart),
        label: "购物车"
    ),
  ];

  void _onItemTap(index){
    setState(() {
      currentIndex = index;
      currentWidget = tabList[currentIndex];
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
      bottomNavigationBar: BottomNavigationBar(
        items: itemList,
        currentIndex: currentIndex,
        onTap: _onItemTap,
        type: BottomNavigationBarType.fixed,
      ),
      body: IndexedStack(
        index: currentIndex,
        children: tabList,
      ),
    );
  }
}
